<?php

namespace App\Containers\AppSection\Ban\Actions;

use App\Containers\AppSection\Ban\Tasks\CreateBanTask;
use App\Containers\AppSection\Log\Tasks\CreateLogTask;
use App\Containers\AppSection\Profile\Tasks\FindLatestProfileByProfileIdTask;
use App\Containers\AppSection\Profile\Tasks\FindProfileByIdTask;
use App\Containers\AppSection\Profile\Tasks\UpdateProfileTask;
use App\Ship\Exceptions\BadRequestException;
use App\Ship\Parents\Actions\Action;
use App\Ship\Parents\Requests\Request;

class BanProfileAction extends Action
{
    public function run($id, Request $request)
    {
        $profile = app(FindProfileByIdTask::class)->run($id);

        if ($profile->isBanned()) {
            throw new BadRequestException('Профиль уже заблокирован!');
        }

        $ban = app(CreateBanTask::class)->run([
            'profile_id' => $profile->id,
            'banned_by_id' => $request->profile()->id,
            'ban_comment' => $request->getInputByKey('comment'),
            'banned_at' => now(),
        ]);

        app(UpdateProfileTask::class)->run($profile->id, ['ban_id' => $ban->id]);

        app(CreateLogTask::class)->run($profile->id, "Профиль №{$profile->id} заблокирован (админ)", 'ban_profile');
        app(CreateLogTask::class)->run($request->profile()->id, "Профиль №{$profile->id} заблокирован", 'ban_profile');
    }
}
